home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / Date.h < prev    next >
C/C++ Source or Header  |  1990-05-19  |  4KB  |  112 lines

  1. #ifndef    DATE_H
  2. #define    DATE_H
  3.  
  4. /*$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Date.h,v 3.0 90/05/20 00:19:24 kgorlen Rel $*/
  5.  
  6. /* Date.h -- declarations for Gregorian calendar dates
  7.  
  8.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  9.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  10.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  11.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  12.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  13.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  14.  
  15. Author:
  16.     K. E. Gorlen
  17.     Computer Systems Laboratory, DCRT
  18.     National Institutes of Health
  19.     Bethesda, MD 20892
  20.  
  21. $Log:    Date.h,v $
  22.  * Revision 3.0  90/05/20  00:19:24  kgorlen
  23.  * Release for 1st edition.
  24.  * 
  25. */
  26.  
  27. #include "Object.h"
  28.  
  29. #ifndef NESTED_TYPES
  30. typedef unsigned short dayTy;
  31. typedef unsigned short monthTy;
  32. typedef unsigned short yearTy;
  33. typedef unsigned long  julTy;
  34. #endif
  35.  
  36. class Date: public VIRTUAL Object {
  37.     DECLARE_MEMBERS(Date);
  38. public:            // type definitions
  39. #ifdef NESTED_TYPES
  40.     typedef unsigned short dayTy;
  41.     typedef unsigned short monthTy;
  42.     typedef unsigned short yearTy;
  43.     typedef unsigned long  julTy;
  44. #endif
  45. public:            // static member functions
  46.     static dayTy dayOfWeek(const char* dayName);
  47.     static bool dayWithinMonth(monthTy month, dayTy day, yearTy year);
  48.     static dayTy daysInYear(yearTy year);
  49.     static julTy jday(monthTy m, dayTy d, yearTy y);
  50.     static bool leapYear(yearTy year);
  51.     static const char* nameOfDay(dayTy weekDayNumber);
  52.     static const char* nameOfMonth(monthTy monthNumber);
  53.     static monthTy numberOfMonth(const char* monthName);
  54. private:        // private member variables
  55.         julTy julnum;   // Julian Day Number (Not same as Julian date.  Jan. 29, 1988 
  56.                         // is not the same as 88029 in Julian Day Number.)
  57.     Date(julTy j)                   { julnum = j; }
  58.     julTy parseDate(istream&);
  59. protected:        // storer() functions for object I/O
  60.     virtual void storer(OIOofd&) const;
  61.     virtual void storer(OIOout&) const;
  62. public:
  63.     Date();                // current date 
  64.     Date(long dayCount);
  65.     Date(long dayCount, yearTy referenceYear);
  66.     Date(dayTy newDay, const char* monthName, yearTy newYear);
  67.     Date(istream&);            // read date from stream 
  68.     bool operator<(const Date& date) const    { return julnum < date.julnum; }
  69.     bool operator<=(const Date& date) const    { return julnum <= date.julnum; }
  70.     bool operator>(const Date& date) const    { return date < *this; }
  71.     bool operator>=(const Date& date) const    { return date <= *this; }
  72.     bool operator==(const Date& date) const    { return julnum == date.julnum; }
  73.     bool operator!=(const Date& date) const    { return julnum != date.julnum; }
  74.     friend Date operator+(const Date& dt, int dd)    { return Date(dt.julnum + dd); }
  75.     friend Date operator+(int dd, const Date& dt)    { return Date(dt.julnum + dd); }
  76.     int operator-(const Date& dt) const    { return (int)(julnum - dt.julnum); }
  77.     Date operator-(int dd) const        { return Date(julnum - dd); }
  78.     void operator+=(int dd)            { julnum += dd; }
  79.     void operator-=(int dd)            { julnum -= dd; }
  80.     bool between(const Date& d1, const Date& d2) const {
  81.         return julnum >= d1.julnum && julnum <= d2.julnum;
  82.     }
  83.     dayTy day() const;
  84.     dayTy dayOfMonth() const;
  85.     dayTy firstDayOfMonth() const        { return firstDayOfMonth(month()); }
  86.     dayTy firstDayOfMonth(monthTy month) const;
  87.     bool leap() const;
  88.     Date max(const Date& dt) const { 
  89.             if (dt.julnum > julnum) return dt;
  90.         else return *this;
  91.         }
  92.     void mdy(monthTy&,dayTy&,yearTy&) const;
  93.     Date min(const Date& dt) const {
  94.             if (dt.julnum < julnum) return dt;
  95.         else return *this;
  96.         }
  97.     monthTy    month() const;
  98.     const char* nameOfMonth() const;
  99.     Date previous(const char* dayName) const;
  100.     dayTy weekDay() const;
  101.     yearTy year() const;
  102.     virtual int compare(const Object&) const;
  103.     virtual void deepenShallowCopy();    // {}
  104.     virtual unsigned hash() const;
  105.     virtual bool isEqual(const Object&) const;
  106.     virtual void printOn(ostream& strm =cout) const;
  107.     virtual void scanFrom(istream& strm);
  108.     virtual const Class* species() const;
  109. };
  110.  
  111. #endif
  112.